package com.adams.user.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.adams.access.db.bean.User;
import com.adams.access.db.dao.hibernate.HibernateDao;
import com.adams.user.dao.UserDao;
import com.adams.utils.Page;

/**
 * 
 * 
 * File Name : UserDaoImpl.java
 * 
 * @Description : 用户管理数据操作接口，提供对用户的增、删、改查功能
 * @author 902990封文进
 */
@Repository("userDao")
public class UserDaoImpl extends HibernateDao<User, Long> implements UserDao
{

	/**
	 * 
	 * Description : 根据用户名，真实姓名和分页查询用户（用户名和真实姓名采用模糊查询）
	 * 
	 * @param page
	 * @param userName
	 * @param realName
	 * @return Page<User>
	 * 
	 */
	public Page<User> queryAllByPage(Page<User> page, String userName,
			String realName)
	{
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotEmpty(userName))
		{
			sb.append("and userName like '%" + userName + "%' ");
		}
		if (StringUtils.isNotEmpty(realName))
		{
			sb.append("and realName like '%" + realName + "%' ");
		}
		return queryPage(page, "from User where 1=1 " + sb.toString());
	}

	/**
	 * 
	 * Description : 根据用户名查询用户
	 * 
	 * @param name
	 * @return User
	 * 
	 */
	public User queryByName(String name)
	{
		List<User> list = query("from User as u where u.userName=?", name);
		if (list != null && list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}

	/**
	 * 
	 * Description : 根据角色查询用户
	 * 
	 * @param roleId
	 * @return List<User>
	 * 
	 */
	public List<User> queryByRoleId(Long roleId)
	{
		return query("from User as u where u.role.id=?", roleId);
	}

}
